package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
//@PreAuthorize("hasRole('ROLE_ADMIN')")
public class PermissionController {

    @Reference
    private PermissionService permissionService;


    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult<Permission> result =null;
        try {
            PageInfo<Permission> menuList = permissionService.findPage(queryPageBean);
            result = new PageResult<>(menuList.getTotal(),menuList.getList());
            System.out.println(result.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "获取权限失败");
        }
        return new Result(true, "获取权限成功",result);
    }


    @PostMapping("/add")
    public Result add(@RequestBody Permission permission){
        permissionService.add(permission);
        return new Result(true, "添加权限成功");
    }

    @PostMapping("/update")
    public Result update(@RequestBody Permission permission){
        permissionService.update(permission);
        return new Result(true, "更新权限成功");
    }


    @PostMapping("/deleteById")
    public Result deleteById(Integer id) {
        permissionService.deleteById(id);
        return new Result(true, "删除权限成功");
    }

//    findkeywords

    @GetMapping("/findkeywords")
    public Result findkeywords() {
        List<String> permissionstrList = permissionService.findkeywords();
        return new Result(true, "查询权限关键字成功",permissionstrList);
    }
//findkeynames

    @GetMapping("/findkeynames")
    public Result findkeynames() {
        List<String> permissionNamestrList = permissionService.findkeynames();
        return new Result(true, "查询权限名称成功",permissionNamestrList);
    }

}
